package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Customers;
import cn.springboot.erp.entity.Products;
import cn.springboot.erp.entity.SalesOrders;
import cn.springboot.erp.mapper.CustomersMapper;
import cn.springboot.erp.mapper.ProductsMapper;
import cn.springboot.erp.mapper.SalesOrdersMapper;
import cn.springboot.erp.vo.SaleOrderDetailsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.SalesOrderDetails;
import cn.springboot.erp.service.SalesOrderDetailsService;
import cn.springboot.erp.mapper.SalesOrderDetailsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【sales_order_details】的数据库操作Service实现
* @createDate 2024-05-12 10:14:04
*/
@Service
public class SalesOrderDetailsServiceImpl extends ServiceImpl<SalesOrderDetailsMapper, SalesOrderDetails>
    implements SalesOrderDetailsService{

    @Resource
    private SalesOrdersMapper salesOrdersMapper;
    @Resource
    private ProductsMapper productsMapper;
    @Resource
    private CustomersMapper customersMapper;

    @Override
    public ResponseResult listPage(QueryParams<SalesOrderDetails> queryParams) {
        // 获取分页数据
        SalesOrderDetails searchParams = queryParams.getSearchParams();
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();

        Page<SalesOrderDetails> salesOrderDetailsPage = new Page<>(page, limit);
        LambdaQueryWrapper<SalesOrderDetails> lambda = new QueryWrapper<SalesOrderDetails>().lambda();
        lambda.eq(ObjectUtils.isNotNull(searchParams.getOrderId()),SalesOrderDetails::getOrderId,searchParams.getOrderId())
                .eq(ObjectUtils.isNotNull(searchParams.getProductId()),SalesOrderDetails::getProductId,searchParams.getProductId())
                .orderByDesc(SalesOrderDetails::getCreateTime);
        baseMapper.selectPage(salesOrderDetailsPage,lambda);

        List<SaleOrderDetailsVO> vo = salesOrderDetailsPage.getRecords().stream().map(order -> {
            SaleOrderDetailsVO saleOrderDetailsVO = new SaleOrderDetailsVO();
            BeanUtils.copyProperties(order,saleOrderDetailsVO);
            // 获取订单id
            Integer orderId = order.getOrderId();
            SalesOrders salesOrders = salesOrdersMapper.selectById(orderId);
            saleOrderDetailsVO.setOrderNumber(salesOrders.getOrderNumber());

            Integer customerId = salesOrders.getCustomerId();
            Customers customers = customersMapper.selectById(customerId);
            saleOrderDetailsVO.setCustomerName(customers.getName());

            // 获取商品id
            Integer productId = order.getProductId();
            Products products = productsMapper.selectById(productId);
            saleOrderDetailsVO.setProducts(products);
            return saleOrderDetailsVO;
        }).collect(Collectors.toList());
        return ResponseResult.Success()
                .data("total",salesOrderDetailsPage.getTotal())
                .data("vo",vo);
    }
}




